//
//  ZSY_NearbyViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 15/12/28.
//  Copyright © 2015年 张诗宇. All rights reserved.
//

#import "ZSY_NearbyViewController.h"
#import "ZSY_AllViewController.h"
#import "ZSY_SceneViewController.h"
#import "ZSY_StayViewController.h"
#import "ZSY_HallViewController.h"
#import "ZSY_EntertainmentViewController.h"
#import "ZSY_ShoppingViewController.h"
#import "ZSY_BaseButton.h"

static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.3;

#define YCKScreenW [UIScreen mainScreen].bounds.size.width
#define YCKScreenH [UIScreen mainScreen].bounds.size.height

@interface ZSY_NearbyViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *titleScrollView;
@property (nonatomic, retain) UIScrollView *contentScrollView;
/**
    选中按钮
 */
@property (nonatomic, retain) ZSY_BaseButton *selTitleButton;
@property (nonatomic, retain) NSMutableArray *buttons;

@end

@implementation ZSY_NearbyViewController

- (void)dealloc{
    [_titleScrollView release];
    [_contentScrollView release];
    [_selTitleButton release];
    [_buttons release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的附近";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_new_poi_back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    [self setupTitleScrollView];    // 创建小scrollView
    [self setupContentScrollView];  // 创建大scrollView
    [self addChildViewController];  // 添加子控制器
    [self setupTitle];              // 根据姿势图个数创建小scrollView得按钮
    self.titleScrollView.backgroundColor = [UIColor lightGrayColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * YCKScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
    
}

#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView {
        // 判断是否存在导航控制器来判断y值
    CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, 0, YCKScreenW, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    // 取消边缘反弹效果
    titleScrollView.bounces = NO;
    [self.view addSubview:titleScrollView];
    self.titleScrollView = titleScrollView;
    
    [titleScrollView release];

}

- (void)leftButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 设置内容
- (void)setupContentScrollView {
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, YCKScreenW, YCKScreenH - navBarH);

    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    // 取消边缘反弹效果
    contentScrollView.bounces = NO;
    [self.view addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
    
    [contentScrollView release];
    
}

#pragma mark - 添加自控制器
- (void)addChildViewController {
    ZSY_AllViewController *allVC = [[ZSY_AllViewController alloc] init];
    allVC.title = @"全部";
    [self addChildViewController:allVC];
    [allVC release];
    
    ZSY_SceneViewController *sceneVC = [[ZSY_SceneViewController alloc] init];
    sceneVC.title = @"景点";
    [self addChildViewController:sceneVC];
    [sceneVC release];
    
    ZSY_StayViewController *stayVC = [[ZSY_StayViewController alloc] init];
    stayVC.title = @"住宿";
    [self addChildViewController:stayVC];
    [stayVC release];
    
    ZSY_HallViewController *hallVC = [[ZSY_HallViewController alloc] init];
    hallVC.title = @"餐厅";
    [self addChildViewController:hallVC];
    [hallVC release];
    
    ZSY_EntertainmentViewController *entertainVC = [[ZSY_EntertainmentViewController alloc] init];
    entertainVC.title = @"休闲娱乐";
    [self addChildViewController:entertainVC];
    [entertainVC release];
    
    ZSY_ShoppingViewController *shopVC = [[ZSY_ShoppingViewController alloc] init];
    shopVC.title = @"购物";
    [self addChildViewController:shopVC];
    [shopVC release];
    
}


#pragma mark - 设置标题
- (void)setupTitle {
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 100;
    CGFloat h = titleH;
    self.buttons = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        ZSY_BaseButton *btn = [ZSY_BaseButton buttonWithType:UIButtonTypeCustom];
        btn.frame = rect;
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        if (i == 0) {
            [self chick:btn];
        }
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

#pragma mark - 按钮点击
- (void)chick:(ZSY_BaseButton *)button {
    [self selectTitleBtn:button];
    
    NSUInteger i = button.tag;
    CGFloat x = i * YCKScreenW;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);

}

#pragma mark - 选中按钮
- (void)selectTitleBtn:(ZSY_BaseButton *)button {
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    self.selTitleButton = button;
    [self setupTitleCenter:button];
}

- (void)setUpOneChildViewController:(NSUInteger)i {
    CGFloat x = i * YCKScreenW;
    ZSY_BaseViewController *vc = self.childViewControllers[i];
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, YCKScreenW, YCKScreenH - self.contentScrollView.frame.origin.y);
    [self.contentScrollView addSubview:vc.view];
}

- (void)setupTitleCenter:(UIButton *)button {
    CGFloat offset = button.center.x - YCKScreenW * 0.5;
    if (offset < 0) {
        offset = 0;
    }
    CGFloat maxOffset = self.titleScrollView.contentSize.width - YCKScreenW;
    if (offset > maxOffset) {
        offset = maxOffset;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSUInteger i = self.contentScrollView.contentOffset.x / YCKScreenW;
    
    [self selectTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger leftIndex = offsetX / YCKScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    UIButton *leftButton = self.buttons[leftIndex];
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / YCKScreenW - leftIndex;
    CGFloat scaleL = 1 - scaleR;
    CGFloat tranScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * tranScale + 1, scaleL * tranScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * tranScale + 1, scaleR * tranScale + 1);
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
